//
//  HYNoNetView.m
//  Chainup
//
//  Created by victor lau on 2018/7/11.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "HYNoNetView.h"
#import "JFKitPri.h"

@interface HYNoNetView ()

@property (strong, nonatomic)  UILabel *titleLabel;
@property (strong, nonatomic)  UIImageView *imageView;
@property (strong, nonatomic)  UIControl *control;

@end

@implementation HYNoNetView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.viewHeight = frame.size.height;
        [self addSubViews];
        [self addConstraints];
       
    }
    return self;
}

- (void)addSubViews{
    [self addSubview:self.control];
    [self addSubview:self.imageView];
    [self addSubview:self.titleLabel];
}

- (void)addConstraints{
    
    AWWeakSelf(self)
    
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(weakself.mas_centerX);
        make.centerY.mas_equalTo(weakself.mas_centerY).offset(-30);
        make.width.mas_equalTo(120);
        make.height.mas_equalTo(120);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.top.mas_equalTo(weakself.imageView.mas_bottom).offset(10);
        make.leading.mas_equalTo(weakself.mas_leading).offset(20);
        make.trailing.mas_equalTo(weakself.mas_trailing).offset(-20);
        make.height.mas_equalTo(@40);
        
    }];
    
}

- (void)setImage:(UIImage *)image{
    self.imageView.image = image;
}

- (void)setTitle:(NSString *)title{
    self.titleLabel.text = title;
}

- (void)retryClick:(UIControl *)sender {
    if(self.delegate && [self.delegate respondsToSelector:@selector(noNetRetryClick)]){
        [self.delegate noNetRetryClick];
    }
}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y,
                            self.frame.size.width, self.viewHeight);
   
    [self layoutIfNeeded];
}

-(void)setEmptyType:(HYEmptyImageType)emptyType{
    
    _emptyType = emptyType;
    if (emptyType == HYEmptyImageTypeNoDat) {
        self.image = [UIImage imageNamed:@"no_data"];
        self.title = NSLocalizedString(@"No_data", nil);
    }else  if (emptyType == HYEmptyImageTypeNoNet) {
        self.image = [UIImage imageNamed:@"no_network"];
        self.title = NSLocalizedString(@"No_Network", nil);
    }else  if (emptyType == HYEmptyImageTypeError) {
        self.image = [UIImage imageNamed:@"error"];
        self.title = NSLocalizedString(@"QError", nil);
    }

}

-(void)setViewHeight:(CGFloat)viewHeight
{
    _viewHeight = viewHeight;
    [self layoutSubviews];
}

#pragma mark -
- (UIControl *)control{
    if (!_control) {
        _control = [[UIControl alloc] initWithFrame:self.frame];
        [_control addTarget:self action:@selector(retryClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _control;
}

- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textColor = [UIColor awColorWithHexString:@"#BFC6D4"];
        _titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size: 13];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLabel;
}

- (UIImageView *)imageView{
    if (!_imageView) {
        _imageView = [[UIImageView alloc] init];
        _imageView.image = [UIImage imageNamed:@""];
    }
    return _imageView;
}







@end
